import lodash from 'lodash';
import dt from 'moment';
import * as data from './data';
import log from '../../../log';
import * as util from '../../../util';

export function fetch(query, page, pageSize) {
    return util.resolve().then(() => {
        log.info('fetch logs params----', query);
        const pageOpt = {
            page,
            pageSize
        };
        let start = '';
        let end = '';
        if (query.startDate) {
            start = `${dt(query.startDate).format('YYYY-MM-DD').toString()} 00:00:00`;
            end = `${dt(query.endDate).format('YYYY-MM-DD').toString()} 23:59:59`;
        }
        delete query.startDate;
        delete query.endDate;
        let _ = [];
        if (lodash.keys(query).length > 0) {
            lodash.mapKeys(query, (value, key) => {
                _.push([`${key}`, '=', `${value}`]);
            });
        }
        if (start.length > 0) {
            _.push(['createTime', '>=', `${start}`]);
            _.push(['createTime', '<=', `${end}`]);
        }
        if (_.length < 1) {
            _ = null;
        }
        return data.fetch(_, 1, pageOpt).then(res => {
            log.info('fetch logs result----', res.toJSON());
            return util.data(res, 1);
        });
    }).catch(err => {
        log.error('fetch logs is err----', err);
        return err;
    });
}
export function find(query) {
    return util.resolve().then(() => {
        log.info('find logs params----', query);
        let start = '';
        let end = '';
        if (query.startDate) {
            start = `${dt(query.startDate).format('YYYY-MM-DD').toString()} 00:00:00`;
            end = `${dt(query.endDate).format('YYYY-MM-DD').toString()} 23:59:59`;
        }
        delete query.startDate;
        delete query.endDate;
        let _ = [];
        if (lodash.keys(query).length > 0) {
            lodash.mapKeys(query, (value, key) => {
                _.push([`${key}`, '=', `${value}`]);
            });
        }
        if (start.length > 0) {
            _.push(['createTime', '>=', `${start}`]);
            _.push(['createTime', '<=', `${end}`]);
        }
        if (_.length < 1) {
            _ = null;
        }
        return data.find(_, 1).then(res => {
            log.info('find logs result----', util.data(res));
            return util.data(res);
        });
    }).catch(err => {
        log.error('find logs is err----', err);
        return err;
    });
}
export function add(modules) {
    return util.resolve().then(() => {
        log.info('add logs params----', modules);
        return data.add(modules).then(res => {
            log.info('add module result----', util.data(res));
            return util.data(res);
        });
    }).catch(err => {
        log.error('add logs is err----', err);
        return err;
    });
}
export function edit(logs) {
    return util.resolve().then(() => {
        log.info('edit logs params----', logs);
        return data.edit(logs).then(res => {
            log.info('edit logs result----', util.data(res));
            return util.data(res);
        });
    }).catch(err => {
        log.error('edit logs is err----', err);
        return err;
    });
}
export function del(id) {
    return util.resolve().then(() => {
        log.info('delete module params----', id);
        return data.del(id).then(res => {
            log.info('delete logs result----success', util.data(res));
            return util.data(res);
        });
    }).catch(err => {
        log.error('delete logs is err----', err);
        return err;
    });
}

